#########################################################################
##
## This file is part of the SAMRAI distribution.  For full copyright 
## information, see COPYRIGHT and LICENSE. 
##
## Copyright:     (c) 1997-2019 Lawrence Livermore National Security, LLC
## Description:   makefile for linear advection sample application 
##
#########################################################################

SAMRAI        = @top_srcdir@
SRCDIR        = @srcdir@
SUBDIR        = source/test/applications/LinAdv
VPATH         = @srcdir@
TESTTOOLS     = ../../testtools
OBJECT        = ../../../..
REPORT        = $(OBJECT)/report.xml
TESTLIBDIR    = $(OBJECT)/source/test/testlib
TESTLIB       = $(TESTLIBDIR)/libSAMRAI_test$(LIB_SUFFIX)

default: check	

include $(OBJECT)/config/Makefile.config

SUBDIRS = fortran

CPPFLAGS_EXTRA = -DTESTING=1 

NUM_TESTS = 11

TEST_NPROCS = @TEST_NPROCS@
QUOTE = \"

CXX_OBJS      = main.o LinAdv.o
F_OBJS        = fortran/flux2d.o \
		fortran/grad2d.o \
		fortran/init2d.o \
		fortran/stable2d.o \
		fortran/stufprobc.o \
		fortran/trace1d.o \
		fortran/trace2d.o \
		fortran/flux3d.o \
		fortran/init3d.o \
		fortran/grad3d.o \
		fortran/trace3d.o \
		fortran/stable3d.o \
		fortran/fluxcorner3d.o

main:	$(CXX_OBJS) $(F_OBJS) $(LIBSAMRAIDEPEND) $(TESTLIB)
	(cd $(TESTLIBDIR) && $(MAKE) library) || exit 1
	for DIR in $(SUBDIRS); do if test -d $$DIR; then (cd $$DIR && $(MAKE) $@) ; fi || exit 1; done
	$(CXX) $(CXXFLAGS) $(LDFLAGS) $(CXX_OBJS) $(F_OBJS) $(TESTLIB) \
	$(LIBSAMRAI3D) $(LIBSAMRAI) $(LDLIBS) -o main

# Prevents "No rule to make target" error.  Built in the rule for main.
$(TESTLIB):

fortran/flux2d.o:

fortran/grad2d.o:

fortran/init2d.o:

fortran/stable2d.o:

fortran/stufprobc.o:

fortran/trace1d.o:

fortran/trace2d.o:

fortran/flux3d.o:

fortran/init3d.o:

fortran/grad3d.o:

fortran/trace3d.o:

fortran/stable3d.o:

fortran/fluxcorner3d.o:

check:
	$(MAKE) check2d
	$(MAKE) check3d

# WARNING: Checking of the redistribute routine is a bit of a problem since it
#          needs only a single run  to have been done.  So remove the old
#          stuff.
check2d:	main
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications LinAdv\" name=$(QUOTE)2d $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test.2d.input | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done
ifeq ($(findstring 4, ${TEST_NPROCS}),4)
	  $(RM) test.2d.restart/restore.000003/nodes.*1;
	  $(RM) test.2d.restart/restore.000003/nodes.*4;
	  $(OBJECT)/config/serpa-run 2 $(OBJECT)/tools/restart/restart-redistribute test.2d.restart test_restart_redistribute_2d 3 4;
	  @echo "    <testcase classname=\"applications LinAdv\" name=$(QUOTE)2d restart redistribute 4 procs$(QUOTE)>" >> $(REPORT);
	  $(OBJECT)/config/serpa-run 4 ./main test_inputs/test.2d.input test_restart_redistribute_2d 3 | $(TEE) foo;
	  @if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi;
	  @echo "    </testcase>" >> $(REPORT);
endif
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications LinAdv\" name=$(QUOTE)2d restart $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test.2d.input test.2d.restart 5 | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications LinAdv\" name=$(QUOTE)2d sync $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test_sync.2d.input | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications LinAdv\" name=$(QUOTE)2d sync restart $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test_sync.2d.input test_sync.2d.restart 5 | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications LinAdv\" name=$(QUOTE)2d sync_restart $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test_sync_restart.2d.input | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications LinAdv\" name=$(QUOTE)2d sync_restart restart $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test_sync_restart.2d.input test_sync_restart.2d.restart 5 | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications LinAdv\" name=$(QUOTE)2d time refinement $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test_time_refinement.2d.input | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done
	$(RM) foo;

check3d:	main
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications LinAdv\" name=$(QUOTE)3d $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test.3d.input | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications LinAdv\" name=$(QUOTE)3d restart $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test.3d.input test.3d.restart 5 | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications LinAdv\" name=$(QUOTE)3d sync $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test_sync.3d.input | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications LinAdv\" name=$(QUOTE)3d sync restart $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test_sync.3d.input test_sync.3d.restart 5 | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done
	$(RM) foo;

checkcompile: main

checktest:
	$(RM) makecheck.logfile
	$(MAKE) check 2>&1 | $(TEE) makecheck.logfile
	$(TESTTOOLS)/testcount.sh $(TEST_NPROCS) $(NUM_TESTS) 1 makecheck.logfile
	$(RM) makecheck.logfile

examples2d:	main
	@for i in example_inputs/*.2d.input ; do	\
	  $(OBJECT)/config/serpa-run $(TEST_NPROCS) ./main $${i};	\
	done

examples3d:	main
	@for i in example_inputs/*.3d.input ; do	\
	  $(OBJECT)/config/serpa-run $(TEST_NPROCS) ./main $${i};	\
	done

examples:
	$(MAKE) examples2d
	$(MAKE) examples3d

perf:

everything:
	$(MAKE) checkcompile || exit 1
	$(MAKE) checktest
	$(MAKE) examples
	$(MAKE) perf

checkclean:
	$(CLEAN_COMMON_CHECK_FILES)
	$(RM) test*restart* viz* *root *json

clean: checkclean
	$(CLEAN_COMMON_TEST_FILES)
	$(RM) main
	for DIR in $(SUBDIRS); do if test -d $$DIR; then (cd $$DIR && $(MAKE) $@) ; fi || exit 1; done

include $(SRCDIR)/Makefile.depend

AutoTester.o:	$(AUTOTEST)/AutoTester.C
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c  $(AUTOTEST)/AutoTester.C -o $@
